<template lang="pug">
  div.table-empty
    s-table(
    v-if="$route.path === '/setting/config/document'"
    ref="table"
    :reqUrl="tableObj.url"
    reqMethod="get"
    :column="tableObj.column"
    :queryPropsParams="{type: 1}"
    :filterProps="{ showBtns: false, fields: [] }"
    )
      .table-operator(slot="table-operator")
        el-button.gradientVertical(size="large" round @click="actionHandle({}, 'add')") 新增
      template(slot="action" slot-scope="text, scope")
        el-link(@click="actionHandle(scope, 'edit')" type="success") 修改
        el-popconfirm(
          title="您确认要删除该项?"
          @confirm="() => actionHandle(scope, 'delete')"
          @onConfirm="() => actionHandle(scope, 'delete')"
        )
          el-link(slot="reference" type="danger") 删除
      template(slot="created" slot-scope="text, scope")
        span {{text | dateFilter('YYYY/MM/DD HH:mm')}}
    router-view(v-else)
</template>

<script>
import { column } from './config'
import { delDocumentDir, dirSubmit } from '@/api/common.js'
export default {
  data () {
    return {
      tableObj: {
        url: 'api/document_dir/list',
        column
      }
    }
  },
  methods: {
    refreshTable (flag = false) {
      this.$refs.table.refresh(flag)
    },

    actionHandle (row, type) {
      if (type === 'delete') {
        // 删除
        this.deleteDir(row.id)
        return
      }
      if (type === 'add') {
        this.editDir('', {})
        return
      }
      if (type === 'edit') {
        this.editDir(row.title, { id: row.id })
        return
      }
    },

    deleteDir (id) {
      delDocumentDir({ id }).then(res => {
        console.log(res)
        this.refreshTable()
      })
    },

    editDir (inputValue, params) {
      this.$prompt('输入分类名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValue: inputValue
      }).then(({ value }) => {
        if (value) {
          dirSubmit({ type: 1, title: value, ...params }).then(res => {
            console.log(res)
            this.$message.success('添加目录成功')
            this.refreshTable()
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .container{

  }
</style>
